package kr.co.bnsmedia.touchconfig;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

public class SideviewActivity extends Activity implements View.OnClickListener, IOnHandlerMessage {

	private static final int RADIO_FUNCTION_PWM = 0;
	private static final int RADIO_FUNCTION_THRESHOLD = 1;
	private static final int RADIO_FUNCTION_YCENTER = 2;

	private static final int HANDLER_SIDEVIEW_START = 1;

	private MyApp mApp = null;
	private WeakRefHandler mHandler = null;
	private SideviewView mSideview = null;

	private int mCameraIdx = 0;
	private int mFunctionIdx = 0;

	private final int BUTTONS [] = {
			R.id.btn_down_10,
			R.id.btn_down_1,
			R.id.btn_pwm,
			R.id.btn_threshold,
			R.id.btn_ycenter,
			R.id.btn_up_1,
			R.id.btn_up_10,
			R.id.btn_camera_1,
			R.id.btn_camera_2,
			R.id.btn_camera_3,
			R.id.btn_camera_4,
			R.id.btn_comfirm,
			R.id.btn_cancel,
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sideview);

		for (int button : BUTTONS) {
			findViewById(button).setOnClickListener(this);
		}
		mSideview = (SideviewView)findViewById(R.id.vw_sideview);

		mApp = (MyApp)getApplication();
		mHandler = new WeakRefHandler((IOnHandlerMessage)this);
		mApp.setHandler(mHandler);

		((TextView)findViewById(R.id.tvw_pwm)).setText("" + mApp.touchConfig().bPWMs[0]);

		((CompoundButton)findViewById(R.id.btn_camera_1)).setChecked(true);
		changeCamera(R.id.btn_camera_1);

		mFunctionIdx = RADIO_FUNCTION_THRESHOLD;
		((CompoundButton)findViewById(R.id.btn_threshold)).setChecked(true);
		changeFunction(R.id.btn_threshold);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

	}

	@Override
	public void onClick(View arg0) {
		final int fId = arg0.getId();

		switch (fId) {
		case R.id.btn_down_10:
			changeValue(-10);
			break;
		case R.id.btn_down_1:
			changeValue(-1);
			break;
		case R.id.btn_pwm:
		case R.id.btn_threshold:
		case R.id.btn_ycenter:
			changeFunction(fId);
			break;
		case R.id.btn_up_1:
			changeValue(1);
			break;
		case R.id.btn_up_10:
			changeValue(10);
			break;
		case R.id.btn_camera_1:
		case R.id.btn_camera_2:
		case R.id.btn_camera_3:
		case R.id.btn_camera_4:
			changeCamera(fId);
			break;
		case R.id.btn_comfirm:
		case R.id.btn_cancel:
			mHandler.sendMessage(Message.obtain(mHandler, HANDLER_SIDEVIEW_START,
					g.device.CAMERA1, ((R.id.btn_comfirm == fId) ? RESULT_OK : RESULT_CANCELED)));
			break;
		}
	}

	@Override
	public void handleMessage(Message msg) {
		switch (msg.what) {
		case MyApp.HANDLER_RESULT:
			handlerResult(msg.arg1, msg.arg2, (byte[])msg.obj);
			break;
		case MyApp.HANDLER_EVENT:
			handlerEvent(msg.arg1, msg.arg2, (byte[])msg.obj);
			break;

		case HANDLER_SIDEVIEW_START:
			if ((g.device.CAMERA1 + g.device.MAX_CAMERA) == msg.arg1) {
				if ((g.device.CAMERA1 <= msg.arg2) && ((g.device.CAMERA1 + g.device.MAX_CAMERA) >= msg.arg2)) {
					byte[] start = {1};
					mApp.wirtePacket(g.command.SIDEVIEW_START, (byte)(msg.arg2), (byte)start.length, start);
				} else {
					setResult(msg.arg2);
					finish();
				}
			} else {
				byte[] stop = {0};
				mApp.wirtePacket(g.command.SIDEVIEW_START, (byte)(msg.arg1), (byte)stop.length, stop);
				mHandler.sendMessageDelayed(Message.obtain(mHandler, HANDLER_SIDEVIEW_START, (msg.arg1 + 1), msg.arg2), 100);
			}
			break;
		}
	}


	private void handlerResult(int aDev, int aDlen, byte[] aData) {
		int command = (int)(aData[0] & 0xFF);

		switch (command) {
		case g.command.SET_PWMS:
			((TextView)findViewById(R.id.tvw_pwm)).setText("" + mApp.touchConfig().bPWMs[0]);
			break;
		case g.command.SET_YCENTER:
			((TextView)findViewById(R.id.tvw_ycenter)).setText("" + mApp.cameraConfig(aDev - g.device.CAMERA1).nYCenter);
			break;

		case g.command.SIDEVIEW_START:
			break;
		}
	}

	private void handlerEvent(int aCmdDev, int aDlen, byte[] aData) {
		int command = (aCmdDev>>16) & 0xFFFF;
		int device = aCmdDev & 0xFF;

		switch (command) {
		case g.command.EVT_IMAGE_SIDEVIEW:
			mSideview.setData(device, aDlen, aData);
			break;
		}
	}

	private void changeCamera(int id) {
		switch (id) {
		case R.id.btn_camera_1:
			mCameraIdx = 0;
			((CompoundButton)findViewById(R.id.btn_camera_2)).setChecked(false);
			((CompoundButton)findViewById(R.id.btn_camera_3)).setChecked(false);
			((CompoundButton)findViewById(R.id.btn_camera_4)).setChecked(false);
			break;
		case R.id.btn_camera_2:
			mCameraIdx = 1;
			((CompoundButton)findViewById(R.id.btn_camera_1)).setChecked(false);
			((CompoundButton)findViewById(R.id.btn_camera_3)).setChecked(false);
			((CompoundButton)findViewById(R.id.btn_camera_4)).setChecked(false);
			break;
		case R.id.btn_camera_3:
			mCameraIdx = 2;
			((CompoundButton)findViewById(R.id.btn_camera_1)).setChecked(false);
			((CompoundButton)findViewById(R.id.btn_camera_2)).setChecked(false);
			((CompoundButton)findViewById(R.id.btn_camera_4)).setChecked(false);
			break;
		case R.id.btn_camera_4:
			mCameraIdx = 3;
			((CompoundButton)findViewById(R.id.btn_camera_1)).setChecked(false);
			((CompoundButton)findViewById(R.id.btn_camera_2)).setChecked(false);
			((CompoundButton)findViewById(R.id.btn_camera_3)).setChecked(false);
			break;
		default:
			return;
		}
		((TextView)findViewById(R.id.tvw_threshold)).setText("" + mSideview.threshold());
		((TextView)findViewById(R.id.tvw_ycenter)).setText("" + mApp.cameraConfig(mCameraIdx).nYCenter);

		mHandler.sendMessage(Message.obtain(mHandler, HANDLER_SIDEVIEW_START, g.device.CAMERA1, (g.device.CAMERA1 + mCameraIdx)));
		mSideview.reset();
	}

	private void changeFunction(int id) {
		switch (id) {
		case R.id.btn_pwm:
			mFunctionIdx = RADIO_FUNCTION_PWM;
			((CompoundButton)findViewById(R.id.btn_threshold)).setChecked(false);
			((CompoundButton)findViewById(R.id.btn_ycenter)).setChecked(false);
			break;
		case R.id.btn_threshold:
			mFunctionIdx = RADIO_FUNCTION_THRESHOLD;
			((CompoundButton)findViewById(R.id.btn_pwm)).setChecked(false);
			((CompoundButton)findViewById(R.id.btn_ycenter)).setChecked(false);
			break;
		case R.id.btn_ycenter:
			mFunctionIdx = RADIO_FUNCTION_YCENTER;
			((CompoundButton)findViewById(R.id.btn_pwm)).setChecked(false);
			((CompoundButton)findViewById(R.id.btn_threshold)).setChecked(false);
			break;
		}
	}

	private void changeValue(int offset) {
		int newValue;

		switch (mFunctionIdx) {
		case RADIO_FUNCTION_PWM:
			newValue = mApp.touchConfig().bPWMs[0] + offset;
			if (g.DATA_PWM_MIN > newValue) {
				newValue = g.DATA_PWM_MIN;
			} else if (g.DATA_PWM_MAX < newValue) {
				newValue = g.DATA_PWM_MAX;
			}
			if (mApp.touchConfig().bPWMs[0] != newValue) {
				byte[] pwms = {(byte)newValue, (byte)newValue, (byte)newValue, (byte)newValue};
				mApp.wirtePacket(g.command.SET_PWMS, g.device.TOUCH, (byte)pwms.length, pwms);
			}
			break;
		case RADIO_FUNCTION_THRESHOLD:
			newValue = mSideview.threshold() + offset;
			if (g.DATA_THRESHOLD_MIN > newValue) {
				newValue = g.DATA_THRESHOLD_MIN;
			} else if (g.DATA_THRESHOLD_MAX < newValue) {
				newValue = g.DATA_THRESHOLD_MAX;
			}
			if (mSideview.threshold() != newValue) {
				((TextView)findViewById(R.id.tvw_threshold)).setText("" + newValue);
				mSideview.setThreshold(newValue);
			}
			break;
		case RADIO_FUNCTION_YCENTER:
			newValue = mApp.cameraConfig(mCameraIdx).nYCenter + offset;
			if (g.DATA_YCENTER_MIN > newValue) {
				newValue = g.DATA_YCENTER_MIN;
			} else if (g.DATA_YCENTER_MAX < newValue) {
				newValue = g.DATA_YCENTER_MAX;
			}
			if (mApp.cameraConfig(mCameraIdx).nYCenter != newValue) {
				byte[] yCenter = PacketData.uShortToByteArray(newValue);
				mApp.wirtePacket(g.command.SET_YCENTER, (byte)(g.device.CAMERA1 + mCameraIdx), (byte)yCenter.length, yCenter);
			}
			break;
		}
	}
}
